package com.example.wangxiang.caiyivideosdkdemo;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.SurfaceTexture;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;

import caiyisdk.android.gpuhandler.filters.PreviewBeautyFilter;
import caiyisdk.android.gpuhandler.video.MagicNormalPlayer;
import caiyisdk.android.gpuhandler.video.MagicPlayer;
import caiyisdk.android.gpuhandler.video.OnErrorListener;
import caiyisdk.android.gpuhandler.video.PreviewVideoSuperposition;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private TextureView textureView;
    private Surface surface;
    private MagicPlayer magicPlayer;
    private PreviewBeautyFilter previewBeautyFilter;
    private String audioFile;
    private PreviewVideoSuperposition videoSuperposition;

    private String srcVideo = "/sdcard/error.mp4";
    private String srcMVFile = "/sdcard/caiyisample/huaban.mp4";
    private String srcAudioFile = "/sdcard/caiyisample/takeme.aac";

    private String desVideo = "/sdcard/error-out.mp4";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textureView = findViewById(R.id.textureView);
        textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
                surface = new Surface(surfaceTexture);
                initPlayer();
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {

            }
        });
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
        }
        previewBeautyFilter = new PreviewBeautyFilter(this);
        videoSuperposition = new PreviewVideoSuperposition(this);
        videoSuperposition.setListener(new PreviewVideoSuperposition.OnVideoSuperpositionListener() {
            @Override
            public void onFinish(boolean b) {
                Log.i(TAG, "合并完成 " + b);
            }

            @Override
            public void onVideoDurationPts(long l) {
                Log.i(TAG, "pts: " + l);
            }
        });
    }

    private void initPlayer() {
        if (magicPlayer != null) {
            magicPlayer.release();
        }
        magicPlayer = new MagicNormalPlayer(textureView);
        magicPlayer.setDataSource(srcVideo);
        magicPlayer.setAudioSource(audioFile);
        magicPlayer.setSurface(surface);
        magicPlayer.setRenderSize(800, 800);
        magicPlayer.setOnErrorListener(new OnErrorListener() {
            @Override
            public boolean onError(int i) {
                return false;
            }
        });
        magicPlayer.prepare();
        magicPlayer.start();
    }

    public void onMvBtn(View view) {
        magicPlayer.stopMVEffect();
        magicPlayer.setMVFile(srcMVFile);
    }

    public void onBeauty(View view) {
        magicPlayer.addFilter(previewBeautyFilter);
    }

    public void onMusic(View view) {
        magicPlayer.stop();
        audioFile = "";
        initPlayer();
    }

    public void onCombine(View view) {
        videoSuperposition.setInputPath(srcVideo);
        videoSuperposition.setOutputPath(desVideo);
        videoSuperposition.setDisplaySize(600, 400);
        videoSuperposition.setMVPath(srcMVFile);
        videoSuperposition.setMusicPath(audioFile);
        videoSuperposition.addFilter(previewBeautyFilter);
        int result = videoSuperposition.prepare();
        if (result == PreviewVideoSuperposition.NO_ERROR) {
            videoSuperposition.superposition();
        } else {
            Log.e(TAG, "error: " + result);
        }
    }
}
